﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NRCP.BusinessEntities;

namespace NRCP.BusinessLogicLayer
{
    public partial class NationalPlanBLL
    {
        public List<NationalPlan> GetContents(Func<NationalPlan, bool> query, string orderBy, int startRowIndex = default(int), int maximumRows = default(int))
        {
            try
            {
                //CustomerContent
                List<NationalPlan> result = _socialShareDatabaseContext.NationalPlans.Where(query).Skip(startRowIndex).Take(maximumRows).ToList();
                return result;
            }
            //handling other exceptions...
            catch (Exception ex)
            {
                //Error handling
                throw ex;
            }
        }

        public NationalPlan GetPlan(Func<NationalPlan, bool> query)
        {
            try
            {
                //CustomerContent
                NationalPlan result = _socialShareDatabaseContext.NationalPlans.FirstOrDefault(query);
                return result;
            }
            //handling other exceptions...
            catch (Exception ex)
            {
                //Error handling
                throw ex;
            }
        }

        public int GetNationalPlanAllCount()
        {
            try
            {
                return _socialShareDatabaseContext.NationalPlans.Count();
            }
            catch (Exception ex)
            {
                //Error handling
                throw ex;
            }
        }

        public List<SampleSchedule> GetMonitoringSchedule(int planId, int divisionId, int monthId)
        {
            //TestSampleInformation defaultSample = new TestSampleInformation { Id=-1};
            var result = from y in _socialShareDatabaseContext.NationalPlanDetailsYearlies
                         join m in _socialShareDatabaseContext.NationalPlanDetailsMonthlies
                         on y.Id equals m.NationalPlanDetailsYearlyId
                         join samples in _socialShareDatabaseContext.TestSampleInformations
                         on m.Id equals samples.NationalPlanDetailsMonthlyId into joined
                         from j in joined.DefaultIfEmpty()
                         where y.NationalPlanId == planId &&
                         y.DivisionId == divisionId &&
                         m.Month == monthId
                         select new { SampleDate=j.AssignedOn,Schedule = j != null ? j.Id : -1, Monthly = m.Id, FishName = y.NationalPlanFish.Fish.Name, PlaceNmae = m.SubDistrict.Name, GroupNmae = y.NationalPlanChemicalGroup.ChemicalGroup.Name, Count = m.Count };
            return result.ToList().ConvertAll((x => new SampleSchedule { SampleDate=x.SampleDate, ScheduleId=x.Schedule, MonthlyId=x.Monthly,FishType = x.FishName, TestType = x.GroupNmae, Upazila = x.PlaceNmae, Count=x.Count }));
        }

        public List<PlanSummary> GetNationalPlanSummaryByUpazila(int planId, int upazilaId)
        {
           List<Report_GetSummaryByUpazila_Result> summary=_socialShareDatabaseContext.Report_GetSummaryByUpazila(planId, upazilaId);
           var results=from s in summary
                       group s by new {s.GroupName, s.FishName} into g
                       select new { g.Key.GroupName, g.Key.FishName, SumTotal=g.Sum(s => s.Total) };

           return results.ToList().ConvertAll(x => new PlanSummary { SampleFish = x.FishName, SampleGroup = x.GroupName, SampleTotal = x.SumTotal.Value});
        }

        #region Reports

        public List<NationalPlan_GetSummaryStat_Result> GetPlanComparisonByCount()
        {
            FishBLL fishBll = new FishBLL();
            List<Fish> fishes=fishBll.GetAllFish();
            //List<string> fish = (from fishList in fishes select fishList.Name).Distinct().ToList<string>();
            List<NationalPlan_GetSummaryStat_Result> result=_socialShareDatabaseContext.NationalPlan_GetSummaryStat();
            List<int?> planyears = (from lst in result select lst.PlanYear).Distinct().ToList<int?>();
            
            foreach(int planyear in planyears)
            {
                
                List<NationalPlan_GetSummaryStat_Result> totalDistByYear = result.Where(obj => obj.PlanYear.Value == planyear).ToList();
                foreach (Fish fish in fishes)
                {
                    if (totalDistByYear.Where(p => p.Name == fish.Name).Count() <= 0)
                    {
                        result.Add(new NationalPlan_GetSummaryStat_Result { PlanYear = planyear, Name = fish.Name, TotalSample=0 });
                    }
                }
               
            }
            return result;
        }

        #endregion

        internal List<PlanSummary> GetNationalPlanSummaryByUpazilaAndMonth(int planId, int upazilaId, int monthId)
        {
            List<Report_GetSummaryByUpazila_Result> summary = _socialShareDatabaseContext.Report_GetSummaryByUpazila(planId, upazilaId);
            List<Report_GetSummaryByUpazila_Result> summaryMonths = summary.Where(obj => obj.Month == monthId).ToList();
            var results = from s in summaryMonths
                          group s by new { s.GroupName, s.FishName } into g
                          select new { g.Key.GroupName, g.Key.FishName, SumTotal = g.Sum(s => s.Total) };

            return results.ToList().ConvertAll(x => new PlanSummary { SampleFish = x.FishName, SampleGroup = x.GroupName, SampleTotal = x.SumTotal.Value });
        }
    }
}